package com.sgb.kjwl.viewmodel.self.publish;

import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.user.DeviceStateEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.repository.SelfRepertory;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.fragment.self.DeviceStateFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MLog;
import com.swgk.core.util.MToast;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.functions.Consumer;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class DeviceStateModel extends BaseViewModel {
    private DeviceStateFragment fragment;
    private SelfRepertory repertory;
    private int page = 1;
    private int size = 10;
    public int total;

    public DeviceStateModel(DeviceStateFragment fragment, SelfRepertory repertory) {
        this.fragment = fragment;
        this.repertory = repertory;
    }

    public void getDeviceStateModel(String state,String text) {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        page = 1;
        repertory.getPublishDevice(state, page, size,text)
                .subscribe(new Consumer<BaseEntity<ListResponse<List<DeviceStateEntity>>>>() {
                    @Override
                    public void accept(BaseEntity<ListResponse<List<DeviceStateEntity>>> listResponseBaseEntity) throws Exception {
                        DialogHelper.dismissProgressDialog();
                        total = listResponseBaseEntity.getData().getTotal();
                        if (total > 0) {
                            fragment.refresh(listResponseBaseEntity.getData().getList());
                        } else {
                            fragment.empty();
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        DialogHelper.dismissProgressDialog();
                        MLog.d(AppConstant.API_LOG, "error " + throwable.getMessage());
                        fragment.empty();
                    }
                });
    }

    public void getDeviceStateMoreModel(String state,String text) {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        page = page + 1;
        repertory.getPublishDevice(state, page, size, text)
                .subscribe(new Consumer<BaseEntity<ListResponse<List<DeviceStateEntity>>>>() {
                    @Override
                    public void accept(BaseEntity<ListResponse<List<DeviceStateEntity>>> listResponseBaseEntity) throws Exception {
                        DialogHelper.dismissProgressDialog();
                        total = listResponseBaseEntity.getData().getTotal();
                        fragment.loadMore(listResponseBaseEntity.getData().getList());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        DialogHelper.dismissProgressDialog();
                        MLog.d(AppConstant.API_LOG, "error " + throwable.getMessage());
                        fragment.loadMoreComplete();
                    }
                });
    }

    public void delModel(String id) {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        Map map = new HashMap();
        map.put("id", id);
        repertory.delDeviceList(map)
                .subscribe(new Consumer<BaseEntity>() {
                    @Override
                    public void accept(BaseEntity baseEntity) throws Exception {
                        DialogHelper.dismissProgressDialog();
                        fragment.delSuccess();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        DialogHelper.dismissProgressDialog();
                        MLog.d(AppConstant.API_LOG, "error " + throwable.getMessage());
                    }
                });
    }

    public void onlinOffModel(String id, String state) {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        Map map = new HashMap();
        map.put("id", id);
        map.put("status", state);
        repertory.deviceOnlineOrOff(map)
                .subscribe(new Consumer<BaseEntity>() {
                    @Override
                    public void accept(BaseEntity baseEntity) throws Exception {
                        DialogHelper.dismissProgressDialog();
                        fragment.onlineOffSuccess(state);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        DialogHelper.dismissProgressDialog();
                        MLog.d(AppConstant.API_LOG, "error " + throwable.getMessage());
                    }
                });
    }

    public void getH5Url(String id) {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getUrlSetting("equipmentDetails").enqueue(new Callback<BaseEntity<H5ReEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    String url = response.body().getData().getUrl() + "?id=" + id;
                    WebViewX5HostActivity.start(fragment.getActivity(), url);
                } else {
                    MToast.showToast(fragment.getActivity(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

}
